<template>
    <div class="warning" :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
    import echarts from 'echarts'
    export default {
        name: 'warning',
        props: {
            className: {
                type: String,
                default: 'warning'
            },
            width: {
                type: String,
                default: '360px'
            },
            height: {
                type: String,
                default: '300px'
            },
            autoResize: {
                type: Boolean,
                default: true
            },
            warningData: {
                type: Object
            }
        },
        data () {
            return {
                chart: null,
                isResize:true,
            }
        },
        methods: {
            initChart() {
                this.chart = echarts.init(document.getElementsByClassName('warning')[0]);
                this.setOptions()
            },
            setOptions({} = {}) {
                this.chart.setOption({
                    title: {
                        text: '预警统计',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b}: {c} ({d}%)'
                    },
                    toolbox: {
                        feature: {
                            saveAsImage: {}
                        }
                    },
                    color: ['#42C0EC', '#7D7AFA'],
                    series: [
                        {
                            name: '预警统计',
                            type: 'pie',
                            center: [175, 160],
                            radius: ['50%', '70%'],
                            avoidLabelOverlap: false,
                            label: {
                                formatter: '{b}{c}'
                            },
                            data: [
                                {value: this.warningData.total - this.warningData.warningTotal, name: `未预警:`},
                                {value: this.warningData.warningTotal, name: `预警中:`}
                            ].sort(function (a, b) { return a.value - b.value; })
                        }
                    ]
                });
                window.onresize = this.chart.resize;
            }
        },
        watch: {
            warningData: {
                handler(val) {
                    this.initChart()
                },
                deep: true
            }
        }
    }
</script>

<style lang="scss" scoped>
.warning{
    margin-top: 20px;
}
</style>